﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using BlueGameComponent;

namespace TouchExplode
{
    class GameScreen : BaseScreen
    {
        public bool Pause { get; set; }
        public bool isPause = false;

        public bool GameOver { get; set; }
        public bool isGameOver = false;

        const float MaxTime = 20.0f;
        static Vector2 ProgressBarPos = new Vector2(360,30);
        static Vector2 ProgressBarOffset = new Vector2(5, 6);

        SpriteFont font;
        Texture2D bg;
        Texture2D progressBar;
        Texture2D progressBarBoder;

        int TickCount = 0;
        int ShapeCount = 0;
        float RemainTime = MaxTime;
        int nScore = 0;
        public int Score
        {
            get { return nScore; }
        }



        public GameScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.6);
            AudioManager.StopMusic();
            //AudioManager.PlayMusic("Sounds/gameMusic");

            EnabledGestures = GestureType.Tap | GestureType.FreeDrag;
            Game1.AdMgr.SetVisible(false);
        }

        public override void LoadContent()
        {
            base.LoadContent();
            bg = ScreenManager.Game.Content.Load<Texture2D>("Textures/backgroud");
            progressBar = ScreenManager.Game.Content.Load<Texture2D>("Textures/ProgressBar");
            progressBarBoder = ScreenManager.Game.Content.Load<Texture2D>("Textures/ProgressBarBorder");
            font = ScreenManager.Game.Content.Load<SpriteFont>("Fonts/SpriteFont1");

            PhysicsWorld.GetInstance().ClearAllShape();
            
        }

        public override void HandleInput(InputState input)
        {
            base.HandleInput(input);

            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {

                PauseGame();
            }
            if (!IsActive)
            {
                return;
            }
        }
       
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (Pause)
            {
                return;
            }
            if (GameOver)
            {
                return;
            }
            TickCount++;

            
            ShapeCount = PhysicsWorld.GetInstance().GetShapeCount();

            if (PhysicsWorld.GetInstance().GetShapeCount() < 20)
            {
                if (0 == TickCount % 5)
                {
                    PhysicsWorld.GetInstance().ProduceShapes();
                }
            }

            PhysicsWorld.GetInstance().Update(gameTime);

            int nDestroyCount = ShapeCount - PhysicsWorld.GetInstance().GetShapeCount();
            if (nDestroyCount > 0)
            {
                RemainTime += 5;
                if (RemainTime > MaxTime)
                {
                    RemainTime = MaxTime;
                }
                nScore += (nDestroyCount-3)*10 +nDestroyCount * 10;
            }
            

            //nScore++;

            if (0 == TickCount % 3)
            {
                if (RemainTime > 0)
                { 
                    RemainTime-=0.1f;
                }
            }

            if (RemainTime<=0)
            {
               ScreenManager.AddScreen(new GameOverScreen(this),null);
            }
        }
         
        public override void Draw(GameTime gameTime)
        {
            //base.Draw(gameTime);
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;
            
            spriteBatch.Begin();
            spriteBatch.Draw(bg, new Vector2(0, 0), Color.White);
            spriteBatch.End();

            PhysicsWorld.GetInstance().Draw(gameTime);

            spriteBatch.Begin();

            //-----------这一段是画倒计时进度条的那个控件，效果还可以

            spriteBatch.Draw(progressBarBoder, ProgressBarPos, Color.White);

            float Len = RemainTime / MaxTime * progressBar.Width;
            Vector2 pos = ProgressBarPos + ProgressBarOffset;
            
            int offset = 0;

            if (Len > offset*2)
            {
                offset = 5;
            }
            else
            {
                offset =(int) Len / 2;
            }
            Rectangle re1 = new Rectangle();
            re1.X = 0;
            re1.Y = 0;
            re1.Width = offset;
            re1.Height = (int)progressBar.Height;
            spriteBatch.Draw(progressBar, pos, re1, Color.White);

            pos.X += offset;
            Rectangle re2 = new Rectangle();
            re2.X = (int)(progressBar.Width - (Len - offset));
            re2.Y = 0;
            re2.Width = (int)(Len - offset);
            re2.Height = (int)progressBar.Height;
            spriteBatch.Draw(progressBar, pos, re2, Color.White);
            //-----------


            //spriteBatch.DrawString(font, "time:" + RemainTime.ToString(), new Vector2(600, 10), Color.Black);
            spriteBatch.DrawString(font, "score:"+nScore.ToString(), new Vector2(20, 10), Color.Black);
            spriteBatch.End();
        }
        
        public void EndGame()
        {
            ScreenManager.AddScreen(new GameOverScreen(this), null);
        }
        public void PauseGame()
        {
            if (GameOver)
            {
                return;
            }
            ScreenManager.AddScreen(new PauseScreen(this), null);
        }
        

               
    }
}
